package cn.bluemobi.dylan.step.moder.db;


import javax.inject.Inject;

import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.rx.RealmObservableFactory;


public class RealmHelper implements DBHelper {

    public static final String DB_NAME = "ruihan.realm";
    private static final int dbCode = 1; //数据库版本号
    private Realm mRealm;


    @Inject
    public RealmHelper() {
        initRealm();
//        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
//                .name(RealmHelper.DB_NAME)
//                .schemaVersion(dbCode)
//                .rxFactory(new RealmObservableFactory()) //支持RxJava
//                .deleteRealmIfMigrationNeeded() //当发现新版本号 删除旧的数据
//                .build();
//        mRealm = Realm.getInstance(realmConfiguration);
//        Realm.setDefaultConfiguration(realmConfiguration);
    }

    private void initRealm() {
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .name(RealmHelper.DB_NAME)
                .schemaVersion(dbCode)
                .rxFactory(new RealmObservableFactory()) //支持RxJava
                .deleteRealmIfMigrationNeeded() //当发现新版本号 删除旧的数据
                .build();
        mRealm = Realm.getInstance(realmConfiguration);
    }

    public Realm getRealm() {
        if (mRealm == null || mRealm.isClosed()) {
            initRealm();
        }
        return mRealm;
    }

//--------------------------------------------------患者相关----------------------------------------------------

}
